Skip to content

Conversation

@whart222
Copy link
Member

@whart222 whart222 commented Jul 9, 2025

Fixes #3513 (partially) .

Summary/Motivation:

The alternative_solutions contrib package includes a rudimentary solution pool object. This PR includes a more comprehensive capability for defining and managing solution pools.

Changes proposed in this PR:

  • PyomoPoolManager - Manages pools for Pyomo solutions
  • SolutionPool* - Classes used to define various solution pools
  • Functions in pyomo.contrib.alternative_solutions now use and return PyomoPoolManager instances.
  • PyomoSolution - Function that returns a Solution object given Pyomo model data

NOTE: This is a WIP PR. I'm submitting this now to help define expectations for finalizing this new capability.

Legal Acknowledgement

By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

@whart222 whart222 requested review from emma58 and michaelbynum July 9, 2025 09:15
@whart222 whart222 marked this pull request as draft July 9, 2025 09:16
whart222 added 3 commits July 9, 2025 05:19
Use the Pyomo Bunch class as an alias for Munch, to avoid
introducing an additional Pyomo dependency.
@whart222
Copy link
Member Author

whart222 commented Jul 9, 2025

Note that this PR now includes updates to the Bunch class defined in pyomo.common. I have been using the public Munch class, but these revisions align the Bunch API with Munch.

whart222 added 10 commits July 9, 2025 06:38
Avoiding use of KW_ONLY, which is an internal mechanism
Using new serialization API, which is simpler. :)
1. Reworking solver matrix logic

2. Fixing test to benchmark against the solution values
@codecov
Copy link

codecov bot commented Jul 10, 2025

Codecov Report

❌ Patch coverage is 91.37577% with 42 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.20%. Comparing base (e173cb5) to head (0d79e42).

Files with missing lines Patch % Lines
pyomo/contrib/alternative_solutions/solnpool.py 90.19% 25 Missing ⚠️
pyomo/contrib/alternative_solutions/solution.py 94.23% 6 Missing ⚠️
...o/contrib/alternative_solutions/gurobi_solnpool.py 90.47% 4 Missing ⚠️
pyomo/contrib/alternative_solutions/balas.py 78.57% 3 Missing ⚠️
pyomo/common/collections/bunch.py 95.23% 1 Missing ⚠️
pyomo/contrib/alternative_solutions/aos_utils.py 93.33% 1 Missing ⚠️
...mo/contrib/alternative_solutions/gurobi_lp_enum.py 92.85% 1 Missing ⚠️
pyomo/contrib/alternative_solutions/lp_enum.py 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3657      +/-   ##
==========================================
- Coverage   89.31%   87.20%   -2.12%     
==========================================
  Files         896      826      -70     
  Lines      103682   102378    -1304     
==========================================
- Hits        92605    89280    -3325     
- Misses      11077    13098    +2021     
Flag Coverage Δ
builders ?
linux 86.97% <91.37%> (-2.08%) ⬇️
linux_other 86.97% <91.37%> (+0.01%) ⬆️
osx 83.12% <91.37%> (+0.02%) ⬆️
win 85.23% <91.37%> (+0.01%) ⬆️
win_other 85.23% <91.37%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Added non-positive error check and value 1 warning for num_solutions in balas
Added num_solution error if num_solutions is non-positive, warning if num_solutions =1
viens-code and others added 8 commits September 5, 2025 15:34
Added more test for pass through parameters and applied black to the files
Solnpool Updates to enforce clarity on what metadata versus pool_config objects describe
1. Using keyword names when adding solutions pools to a pool
manager

2. Many documentation changes

3. Renamed Variable and Objective classes to avoid conflicts
with Pyomo naming (in sphinx)

4. Clarifyied the semantics of the gurobi enumeration methods.
Includes documentation with solution pools
@whart222 whart222 changed the title WIP: Solution Pool Solution Pool Sep 19, 2025
@whart222
Copy link
Member Author

@emma58 @michaelbynum FYI, this PR is ready for review.

@blnicho blnicho marked this pull request as ready for review September 22, 2025 21:27
@mrmundt
Copy link
Contributor

mrmundt commented Oct 7, 2025

@whart222 - Can you please resolve the merge conflicts?

@whart222
Copy link
Member Author

@mrmundt @emma58 @michaelbynum Merge conflicts have been resolved.

@whart222
Copy link
Member Author

@emma58 @michaelbynum FYI, a CMU collaborator recently highlighted that the approval of this PR is blocking CMU software commits to snoglode that would leverage this capability.

Copy link
Contributor

@mrmundt mrmundt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is by no means comprehensive, but I wanted to provide at least some feedback to start.

Comment on lines 42 to 50
def test_non_positive_num_solutions(self, mip_solver):
"""
Confirm that an exception is thrown with a non-positive num solutions
"""
m = tc.get_triangle_ip()
try:
enumerate_binary_solutions(m, num_solutions=-1, solver=mip_solver)
except AssertionError as e:
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't really make sense to me. If you want to actually check that an exception is thrown, wouldn't it be better to use something like:

with self.assertRaises(WhateverException):
   --- do the thing ---

Comment on lines 39 to 42
try:
gurobi_enumerate_linear_solutions(n, num_solutions=-1)
except AssertionError as e:
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

Comment on lines 42 to 55
try:
gurobi_generate_solutions(m, num_solutions=-1)
except AssertionError as e:
pass

def test_search_mode(self):
"""
Confirm that an exception is thrown with pool_search_mode not in [1,2]
"""
m = tc.get_triangle_ip()
try:
gurobi_generate_solutions(m, pool_search_mode=0)
except AssertionError as e:
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above

Comment on lines 50 to 53
try:
lp_enum.enumerate_linear_solutions(m, num_solutions=-1, solver=mip_solver)
except AssertionError as e:
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

Copy link
Contributor

@emma58 emma58 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the huuuuge number of comments, but there's a lot here! Many are minor, but there are a few design questions too: The biggest one is that I'm wondering if the VariableInfo and ObjectiveInfo classes could store a reference to the original pyomo component rather than copying a lot but not all of the same data. It seems like that would make it much easier to map solutions in the pool to the original model, which I imagine will be a common user need.

Comment on lines +11 to +19
* alternative optimal solutions can be used to assess trade-offs between
competing objectives;
* comparisons amongst alternative solutions provide
insights into the efficacy of model predictions with
inaccurate or untrusted optimization formulations; or
* alternative solutions can be identified to support the future analysis of model revisions (e.g. to
account for previously unexpressed constraints).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like there is also just the obvious use case of wanting to enable a decision maker to explore the optimal space: for example, for a design optimization.

be configured with solver names and options, and they return a pool of
solutions for the pyomo model. However, these functions are independent
of pyomo's solver interface because they return a custom solution object.
of pyomo's solver interface because they return a custom pool manager object.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: interface -> interfaces

* ``enumerate_linear_solutions``
* :py:func:`enumerate_linear_solutions`

* Finds alternative optimal solutions for a (mixed-integer) linear program.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only one that doesn't say how it generates alternative optimal solutions.

"""
# TODO: this does not set an active pool, should we do that?
# TODO: this does not seem to update the counter value, possibly leading to non-unique ids
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still true? Does that break any of your assumptions in this class?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a problem. I need to think about this. :(

Comment on lines 973 to 979
class PyomoPoolManager(PoolManager):
"""
A subclass of PoolManager for handing pools of Pyomo solutions.
This class redefines the add_pool method to use the _as_pyomo_solution method to construct Solution objects.
Otherwise, this class inherits from PoolManager.
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make the converstion-to-solution method an argument to the PoolManager constructor, and then you don't need this class at all--that can just be changed to _as_pyomo_solution to get this functionality.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting question. Would we ever want to have a pool manager manage pools with different as_solution functions? Right now, that's supported. But I'm not sure whether that makes sense. I'll discuss this with the devs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There still seems a motivation for PyomoPoolManager, to avoid exposing the _as_pyomo_solution function. This is a matter of design, though. I think it's simpler for an end-user to use a class with a pre-defined configuration like this, than to use a base class with explicit arguments. In absolute terms, the same # of symbols are exposed to the user, but the semantics are simpler for PyomoPoolManager (i.e. they don't need to think about the as_solution option).

Comment on lines 59 to 61
as_solution : Function or None
Method for converting inputs into Solution objects.
A value of None will result in the default _as_solution function being used.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this callback design for its extensibility (and because in a lot of contexts we really only care about part of a solution, not every Var in the model, and this will allow for defining that as a "solution" for AOS purposes.). Can you document somewhere what the expected input and output for this function are? It seems like it is assumed it returns a Solution object, right? And it can actually be called given anything because it will pass through what is given to add?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this needs better documentation.

I've renamed _as_solution to default_as_solution, and I've added documentation there.

I've also simplified the API, so that this function only is responsible for constructing and returning a solution object. I moved the checks for the special case where a Solution is passed-in to the add() method.

@whart222
Copy link
Member Author

whart222 commented Nov 6, 2025

@emma58 Thanks for all the feedback! I've gone through some of your comments, but I'll get back to this sometime next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding solution pool functionality to Pyomo

5 participants